package com.sinai.mshab.client.widgets;

import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.Frame;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;

/**
 * Creates a helper text widget which opens up a window to show a url.
 * 
 * @author serge
 * 
 */
public class HelperText extends Label {

	/**
	 * The url which to display.
	 */
	private String url;

	/**
	 * The actual box which shows the help.
	 */
	private DialogBox dialog;

	/**
	 * Creates a new helper text widget.
	 * 
	 * @param name
	 *            the name to display on the label
	 * @param url
	 *            the url that the label points to.
	 */
	public HelperText(String name, String url) {
		this.url = url;
		
		// set up the label
		this.setText(name);		
		this.setStyleName("mshab-helperText");
		this.addClickListener(new HelperListener());
		this.setTitle("Assistance with Field");
		
		// set up the dialog box
		dialog = new MyDialog(url);
		dialog.setAnimationEnabled(true);
	}

	private class HelperListener implements ClickListener {

		public void onClick(Widget sender) {
			dialog.setPopupPosition(getAbsoluteLeft() + 120, getAbsoluteTop() + 20);
			dialog.show();
		}
	}

	/**
	 * A simple dialog box that displays a message, a Frame, and a close button.
	 */
	private static class MyDialog extends DialogBox implements ClickListener {
		public MyDialog(String url) {
			setStyleName("mshab-HelperDialog");
			setText("Assistance With Field");

			Button closeButton = new Button("Close", this);
			HTML msg = new HTML(
					"<center>This is an example of a standard dialog box component.</center>",
					true);

			DockPanel dock = new DockPanel();
			dock.setSpacing(4);

			Frame frame = new Frame(url); 
			frame.setWidth("490px");
			frame.setHeight("390px");
			frame.setStyleName("mshab-helperFrame");
			
			dock.add(closeButton, DockPanel.SOUTH);
			dock.add(frame, DockPanel.NORTH);

			dock.setCellHorizontalAlignment(closeButton, DockPanel.ALIGN_RIGHT);
			dock.setWidth("100%");
			setWidget(dock);
		}

		public void onClick(Widget sender) {
			hide();
		}
	}
}
